package com.ccc.boot.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ccc.boot.base.BaseController;
import com.ccc.boot.constant.CommonConstant;
import com.ccc.boot.exception.BusinessException;
import com.ccc.boot.pojo.Query;
import com.ccc.boot.pojo.UserVO;
import com.ccc.boot.response.ResponseResult;
import com.ccc.boot.system.entity.SysUser;
import com.ccc.boot.system.pojo.PasswordParam;
import com.ccc.boot.system.pojo.RelateUserVO;
import com.ccc.boot.system.pojo.SysUserData;
import com.ccc.boot.system.service.SysUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 人员——REST接口
 *
 * @author superC
 * @version 0.1.0
 * @date 2022/3/24
 */

@RestController
@RequestMapping(CommonConstant.SYS_URI_PREFIX + "/user")
public class SysUserController extends BaseController<SysUser, SysUserService> {

    /**
     * 切换部门
     *
     * @param id     用户主键ID
     * @param deptId 部门主键ID
     * @return {@link ResponseResult}
     * @date 2022/4/1 14:40
     * @author superC
     */
    @PutMapping("/updateDept/{id}/{deptId}")
    public ResponseResult updateDept(@PathVariable Long id, @PathVariable Long deptId) {
        try {
            boolean result = service.updateDept(id, deptId);
            if (result) {
                return ResponseResult.success("切换部门成功");
            }
        } catch (Exception e) {
            logger.error("【捕获异常-修改密码操作】\r\n异常记录：", e);
            throw new BusinessException(e.getMessage());
        }
        return ResponseResult.failure("切换部门失败");
    }

    /**
     * 修改密码
     *
     * @date 2022/4/6 15:49
     * @param param
     * @return {@link ResponseResult}
     * @author superC
     */
    @PutMapping("/updatePassword")
    public ResponseResult updatePassword(@RequestBody PasswordParam param) {
        try {
            boolean result = service.updatePassword(param);
            if (result) {
                return ResponseResult.success("修改密码成功");
            }
        } catch (Exception e) {
            logger.error("【捕获异常-修改密码操作】\r\n异常记录：", e);
            throw new BusinessException(e.getMessage());
        }
        return ResponseResult.failure("修改密码失败");
    }

    /**
     * 分页
     *
     * @param params    包含page, pageSize, 以及其他查询条件字段集，key为字段名，value为其对应的值
     * @return {@link ResponseResult}
     * @date 2022/4/6 15:41
     * @author superC
     */
    @GetMapping("/pageUser")
    public ResponseResult pageUser(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        IPage<UserVO> iPage = service.pageUser(query);
        return ResponseResult.success("获取数据成功", iPage);
    }

    /**
     * 获取所有启用状态的用户列表
     *
     * @param query
     * @return {@link ResponseResult}
     * @date 2022/3/31 17:46
     * @author superC
     */
    @PostMapping("/getEnabledUsers")
    public ResponseResult getEnabledUsers(@RequestBody UserVO query) {
        List<UserVO> users = service.getEnabledUsers(query);
        return ResponseResult.success("获取数据成功", users);
    }

    /**
     * 根据部门ID，获取用户列表，并判断是否在指定roleId下有关联
     *
     * @param roleId  角色ID
     * @return {@link ResponseResult}
     * @date 2022/3/31 17:48
     * @author superC
     */
    @GetMapping("/getUsersByDept/{roleId}")
    public ResponseResult getUsersByDept(@PathVariable Long roleId, @RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        IPage<RelateUserVO> iPage = service.getUsersByDept(roleId, query);
        return ResponseResult.success("获取数据成功", iPage);
    }

    /**
     * 根据角色ID，获取用户列表
     *
     * @param roleId
     * @return {@link ResponseResult}
     * @date 2022/3/31 17:48
     * @author superC
     */
    @GetMapping("/getUsersByRole/{roleId}")
    public ResponseResult getUsersByRole(@PathVariable Long roleId, @RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        IPage<UserVO> iPage = service.getUsersByRole(roleId, query);
        return ResponseResult.success("获取数据成功", iPage);
    }

    /**
     * 获取人员信息
     *
     * @date 2022/4/7 17:30
     * @param id
     * @return {@link ResponseResult}
     * @author superC
     */
    @Override
    @GetMapping("/{id}")
    public ResponseResult get(@PathVariable Long id) {
        SysUserData entity = service.getUser(id);
        return ResponseResult.success("获取数据成功", entity);
    }

    @PostMapping("/{export}")
    public void export(@RequestBody List<UserVO> list) {
        service.export(list, response);
    }

    @PostMapping("/{exportAll}")
    public void exportAll() {
        service.exportAll(response);
    }

}